package com.yylending.las.lending.quota.baseinfo;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.yyfax.common.CLog;
import com.yyfax.common.form.FormRow;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.Constants;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.YJKD;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.common.location.entity.Location;
import com.yylending.las.common.utils.area.AddressCityBean;
import com.yylending.las.common.utils.area.AddressDistrictBean;
import com.yylending.las.common.utils.area.AddressProvinceBean;
import com.yylending.las.common.widgets.pickerview.view.OptionsPickerView;
import com.yylending.las.lending.quota.ApplyQuotaActivity;
import com.yylending.las.lending.quota.entity.CustomerBean;
import com.yylending.las.lending.quota.entity.OptionBean;
import com.yylending.las.lending.quota.entity.OptionsUtil;
import com.yylending.las.lending.quota.entity.PersonalBean;
import com.yylending.las.lending.quota.initdata.InitDataModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by HenryZhang on 2016/11/9.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class InfoFragment extends BaseFragment implements BaseInfoPresenter.View, CustomerInfoPresenter.View {
	private final static String TAG = "InfoFragment";

	final static String ARG_LOCATION = "arg_location";

	private final static int LOADER_ID_QUOTA = 1;
	private final static int LOADER_ID_CUSTOMER_INFO = 3;
	private final static String EMPLOYEE = "1";//上班族
	private final static String ENTREPRENEUR = "2";//企业主
	private final static String HAVE = "2";//有
	private final static String WITHOUT = "1";//无
	private final static String KEY_LOCATION = "location";
	private final static String KEY_BASE_INFO = "baseInfo";

	@BindView(R.id.apply_quota_address_detail)
	FormRow mAddressDetail;
	@BindView(R.id.apply_quota_work_radio)
	RadioGroup mWork;
	@BindView(R.id.apply_quota_work)
	RelativeLayout mWorkRl;
	@BindView(R.id.apply_quota_house_radio)
	RadioGroup mHouse;
	@BindView(R.id.apply_quota_house)
	RelativeLayout mHouseRl;
	@BindView(R.id.apply_quota_education_line)
	View mEducationLine;
	@BindView(R.id.apply_quota_marriage_line)
	View mMarriageLine;
	@BindView(R.id.apply_quota_department_line)
	View mDepartmentLine;
	@BindView(R.id.apply_quota_company_title_line)
	View mCompanyTitleLine;
	@BindView(R.id.apply_quota_notopen)
	RelativeLayout mNotOpenRl;
	@BindView(R.id.apply_quota_education_tv)
	FormRow mEducation;
	@BindView(R.id.apply_quota_marriage_tv)
	FormRow mMarriage;
	@BindView(R.id.apply_quota_housing_tv)
	FormRow mHousing;
	@BindView(R.id.apply_quota_company_detail)
	FormRow mCompanyDetail;
	@BindView(R.id.apply_quota_department_detail)
	FormRow mDepartmentDetail;
	@BindView(R.id.apply_quota_company_title_detail)
	FormRow mCompanyTitleDetail;
	@BindView(R.id.apply_quota_next)
	Button mNext;
	@BindView(R.id.apply_quota_wheel_view_masker)
	View mMasker;
	Unbinder unbinder;
	@BindView(R.id.no_network_layout)
	RelativeLayout mNoNetworkLayout;
	@BindView(R.id.apply_quota_work_radio_entrepreneur)
	RadioButton mRadioEntrepreneur;
	@BindView(R.id.apply_quota_work_radio_salaryman)
	RadioButton mRadioSalaryman;

	/**
	 * 选项卡
	 */
	private OptionsPickerView mOptions;
	/**
	 * 选项卡
	 */
	private OptionsPickerView mAreaOptions;
	/**
	 * 教育程度选项卡
	 */
	private ArrayList<OptionBean> mEducations = new ArrayList<>();
	/**
	 * 婚姻状态选项卡
	 */
	private ArrayList<OptionBean> mMarriages = new ArrayList<>();
	/**
	 * 居住状态选项卡
	 */
	private ArrayList<OptionBean> mLivingStates = new ArrayList<>();
	/**
	 * 常住地址省份
	 */
	private ArrayList<AddressProvinceBean> mProvince = new ArrayList<>();
	/**
	 * 常住地址城市
	 */
	private ArrayList<ArrayList<AddressCityBean>> mCity = new ArrayList<>();
	/**
	 * 常住地址县区
	 */
	private ArrayList<ArrayList<ArrayList<AddressDistrictBean>>> mCounty = new ArrayList<>();
	/**
	 * 已选学历
	 */
	private OptionBean mSelectedEducation;
	/**
	 * 已选婚姻状态
	 */
	private OptionBean mSelectedMarriage;
	/**
	 * 已选住房状态
	 */
	private OptionBean mSelectedHousing;
	private String mSelectedProvince;
	private String mSelectedCity;
	private String mSelectedCounty;

	private BaseInfoPresenter mBaseInfoPresenter;
	private CustomerInfoPresenter mCustomerInfoPresenter;

	/**
	 * 定位信息
	 */
	private Location mLocation;
	/**
	 * 是否是开放地区
	 */
	private boolean mAreaOpened = false;
	/**
	 * 开放城市信息
	 */
	private BaseInfoModel.OpenCity mOpenCity;
	private boolean mInitCompleted;
	/**
	 * 已填写信息
	 */
	private PersonalBean mBaseInfo, mServerData;

	public static BaseFragment newInstance(int sectionNumber, Location location) {
		BaseFragment fragment = new InfoFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_SECTION_NUMBER, sectionNumber);
		args.putParcelable(ARG_LOCATION, location);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		CLog.i(TAG, "InfoFragment onCreate()");
		Bundle bundle = getArguments();
		if (bundle != null) {
			mLocation = bundle.getParcelable(ARG_LOCATION);
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putParcelable(KEY_LOCATION, mLocation);
		//outState.putBoolean(KEY_AREA_OPENED, mAreaOpened);
		//outState.putBoolean(KEY_INIT_COMPLETE, mInitCompleted);
		setBaseInfo();
		outState.putParcelable(KEY_BASE_INFO, mBaseInfo);
	}

	private void setBaseInfo() {
		mBaseInfo = new PersonalBean();
		boolean isEmpty = true;
		if (!TextUtils.isEmpty(mSelectedProvince)) {
			mBaseInfo.addP = mSelectedProvince;
			isEmpty = false;
		}
		if (!TextUtils.isEmpty(mSelectedCity)) {
			mBaseInfo.addC = mSelectedCity;
			isEmpty = false;
		}
		if (!TextUtils.isEmpty(mSelectedCounty)) {
			mBaseInfo.addD = mSelectedCounty;
			isEmpty = false;
		}
		switch (mWork.getCheckedRadioButtonId()) {
			case R.id.apply_quota_work_radio_entrepreneur:
				mBaseInfo.workType = ENTREPRENEUR;
				break;
			case R.id.apply_quota_work_radio_salaryman:
				mBaseInfo.workType = EMPLOYEE;
				break;
		}
		switch (mHouse.getCheckedRadioButtonId()) {
			case R.id.apply_quota_house_none:
				mBaseInfo.hasHouse = WITHOUT;
				break;
			case R.id.apply_quota_house_have:
				mBaseInfo.hasHouse = HAVE;
				break;
		}
		if (null != mSelectedEducation) {
			mBaseInfo.education = mSelectedEducation.itemNo;
		}
		if (null != mSelectedMarriage) {
			mBaseInfo.marriage = mSelectedMarriage.itemNo;
		}
		if (null != mSelectedHousing) {
			mBaseInfo.living = mSelectedHousing.itemNo;
		}
		if (!TextUtils.isEmpty(mDepartmentDetail.getText())) {
			mBaseInfo.dept = mDepartmentDetail.getText();
			isEmpty = false;
		}
		if (!TextUtils.isEmpty(mCompanyDetail.getText())) {
			mBaseInfo.company = mCompanyDetail.getText();
			isEmpty = false;
		}
		if (!TextUtils.isEmpty(mCompanyTitleDetail.getText())) {
			mBaseInfo.title = mCompanyTitleDetail.getText();
			isEmpty = false;
		}
		if (isEmpty) {
			mBaseInfo = null;
		}
	}

	@Override
	public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
		super.onViewStateRestored(savedInstanceState);
		//readBundle(savedInstanceState);
	}

	private void readBundle(Bundle bundle) {
		if (bundle != null) {
			mLocation = bundle.getParcelable(KEY_LOCATION);
			//mAreaOpened = bundle.getBoolean(KEY_AREA_OPENED);
			//mInitCompleted = bundle.getBoolean(KEY_INIT_COMPLETE);
			mBaseInfo = bundle.getParcelable(KEY_BASE_INFO);
			if (null != mBaseInfo)
				CLog.i(TAG, "恢复数据->" + mBaseInfo.toString());
		}
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		switch (id) {
			case LOADER_ID_QUOTA:
				return new PresenterLoader<>(getActivity(), new BaseInfoPresenter.Generator());
			case LOADER_ID_CUSTOMER_INFO:
				return new PresenterLoader<>(getActivity(), new CustomerInfoPresenter.Generator());
			default:
				return null;
		}
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		switch (loader.getId()) {
			case LOADER_ID_QUOTA:
				CLog.i(TAG, "onLoadFinished() BaseInfoPresenter");
				mBaseInfoPresenter = (BaseInfoPresenter) data;
				mBaseInfoPresenter.attach(this);
				break;
			case LOADER_ID_CUSTOMER_INFO:
				CLog.i(TAG, "onLoadFinished() CustomerInfoPresenter");
				mCustomerInfoPresenter = (CustomerInfoPresenter) data;
				mCustomerInfoPresenter.attach(this);
				mEducation.setText("");
				mMarriage.setText("");
				mHousing.setText("");
				showLoading(true);
				mCustomerInfoPresenter.getCustomerInfo(Constants.CUSTOMER_TYPE_BASEINFO);
				break;
		}
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		CLog.i(TAG, "InfoFragment onActivityCreated()");
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		getLoaderManager().initLoader(LOADER_ID_QUOTA, null, this);
		getLoaderManager().initLoader(LOADER_ID_CUSTOMER_INFO, null, this);
	}

	@Override
	public void onDetach() {
		super.onDetach();
		CLog.i(TAG, "InfoFragment onDetach()");
		getLoaderManager().destroyLoader(LOADER_ID_QUOTA);
		getLoaderManager().destroyLoader(LOADER_ID_CUSTOMER_INFO);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		CLog.i(TAG, "InfoFragment onCreatingView()");
		View v = inflater.inflate(R.layout.fragment_quota_info, null);
		unbinder = ButterKnife.bind(this, v);
		initListeners();
		//选项选择器
		mOptions = new OptionsPickerView(getActivity());
		mOptions.setCancelable(true);
		mAreaOptions = new OptionsPickerView(getActivity());
		mAreaOptions.setCancelable(true);
		if (null != savedInstanceState) {
			readBundle(savedInstanceState);
		}
		initOptions();
		return v;
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	private void initListeners() {
		mWork.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (!mAreaOpened) {
					showOrHideViews(View.GONE);
					showOrHideNext(View.INVISIBLE);
					return;
				}
				showOrHideViews(View.VISIBLE);
				showOrHideNext(View.VISIBLE);
			}
		});
	}

	private void initData() {
		CLog.i(TAG, "InfoFragment initData()");
		mEducation.setText("");
		mMarriage.setText("");
		mHousing.setText("");
		showBaseInfo();
		dismissLoading();
	}

	/**
	 * 显示或隐藏工作类型
	 *
	 * @param visible
	 */
	private void showOrHideWork(int visible) {
		mWorkRl.setVisibility(visible);
	}

	/**
	 * 显示或隐藏除工作类型、部门信息和职务名字之外的布局
	 *
	 * @param visible
	 */
	private void showOrHideViews(int visible) {
		mHouseRl.setVisibility(visible);
		mEducationLine.setVisibility(visible);
		mEducation.setVisibility(visible);
		mMarriageLine.setVisibility(visible);
		mMarriage.setVisibility(visible);
		mHousing.setVisibility(visible);
		mCompanyDetail.setVisibility(visible);
	}

	private void showOrHideNext(int visible) {
		mNext.setVisibility(visible);
	}

	@OnClick({R.id.apply_quota_address_detail, R.id.apply_quota_education_tv, R.id.apply_quota_marriage_tv,
			R.id.apply_quota_housing_tv, R.id.apply_quota_next, R.id.no_network_layout})
	public void onViewClicked(View v) {
		switch (v.getId()) {
			case R.id.apply_quota_address_detail: {
				YJKD.hideInputWindow(getActivity());
				if (mProvince.size() == 0) {
					return;
				}
				mAreaOptions.setPicker(mProvince, mCity, mCounty, true);
				mAreaOptions.setTitle("");
				mAreaOptions.setCyclic(false, false, false);
				int provinceIndex = 0;
				int cityIndex = 0;
				int countyIndex = 0;
				if (null != mOpenCity) {
					provinceIndex = mOpenCity.provinceIndex;
					cityIndex = mOpenCity.cityIndex;
					countyIndex = mOpenCity.districtIndex;
				}
				mAreaOptions.setSelectOptions(provinceIndex, cityIndex, countyIndex);
				mAreaOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
					@Override
					public void onOptionsSelect(int options1, int option2, int options3) {
						String address = mProvince.get(options1).getPickerViewText()
								+ " " + mCity.get(options1).get(option2).getPickerViewText();
						if (mCity.get(options1).get(option2).getCounties().size() > 0) {
							address += " " + mCounty.get(options1).get(option2).get(options3).getPickerViewText();
							mSelectedCounty = mCounty.get(options1).get(option2).get(options3).getPickerViewText();
						}else{
							mSelectedCounty = "";
						}
						if (!TextUtils.isEmpty(address)) {
							mSelectedProvince = mProvince.get(options1).getPickerViewText();
							mSelectedCity = mCity.get(options1).get(option2).getPickerViewText();

							if (null == mOpenCity) {
								mOpenCity = new BaseInfoModel.OpenCity();
							}
							mOpenCity.provinceCode = mProvince.get(options1).getAreaId();
							mOpenCity.provinceIndex = options1;
							mOpenCity.cityCode = mCity.get(options1).get(option2).getAreaId();
							mOpenCity.cityIndex = option2;
							if(TextUtils.isEmpty(mSelectedCounty)){
								mOpenCity.districtCode = "";
								mOpenCity.districtIndex = -1;
							}else{
								mOpenCity.districtCode = mCounty.get(options1).get(option2).get(options3).getAreaId();
								mOpenCity.districtIndex = options3;
							}
							mAreaOpened = true;
							mAddressDetail.setText(address);
							enableNext();
						}
						mMasker.setVisibility(View.GONE);
					}
				});
				mAreaOptions.show();
				break;
			}
			case R.id.apply_quota_education_tv: {
				YJKD.hideInputWindow(getActivity());
				if (mEducations.size() == 0) {
					return;
				}
				mOptions.setPicker(mEducations, null, null, false);
				mOptions.setTitle("");
				mOptions.setCyclic(false, false, false);
				int index = OptionsUtil.getOptionIndexByLabel(mEducations, mSelectedEducation == null ? null : mSelectedEducation.itemName);
				mOptions.setSelectOptions(index, -1, -1);
				mOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
					@Override
					public void onOptionsSelect(int options1, int option2, int options3) {
						mSelectedEducation = mEducations.get(options1);
						if (null != mSelectedEducation) {
							mEducation.setText(mSelectedEducation.itemName);
						}
						mMasker.setVisibility(View.GONE);
					}
				});
				mOptions.show();
				break;
			}
			case R.id.apply_quota_marriage_tv: {
				YJKD.hideInputWindow(getActivity());
				if (mMarriages.size() == 0) {
					return;
				}
				mOptions.setPicker(mMarriages, null, null, false);
				mOptions.setTitle("");
				mOptions.setCyclic(false, false, false);
				int index = OptionsUtil.getOptionIndexByLabel(mMarriages, mSelectedMarriage == null ? null : mSelectedMarriage.itemName);
				mOptions.setSelectOptions(index, -1, -1);
				mOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
					@Override
					public void onOptionsSelect(int options1, int option2, int options3) {
						mSelectedMarriage = mMarriages.get(options1);
						if (null != mSelectedMarriage) {
							mMarriage.setText(mSelectedMarriage.itemName);
						}
						mMasker.setVisibility(View.GONE);
					}
				});
				mOptions.show();
				break;
			}
			case R.id.apply_quota_housing_tv: {
				YJKD.hideInputWindow(getActivity());
				if (mLivingStates.size() == 0) {
					return;
				}
				mOptions.setPicker(mLivingStates, null, null, false);
				mOptions.setTitle("");
				mOptions.setCyclic(false, false, false);
				int index = OptionsUtil.getOptionIndexByLabel(mLivingStates, mSelectedHousing == null ? null : mSelectedHousing.itemName);
				mOptions.setSelectOptions(index, -1, -1);
				mOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
					@Override
					public void onOptionsSelect(int options1, int option2, int options3) {
						mSelectedHousing = mLivingStates.get(options1);
						if (null != mSelectedHousing) {
							mHousing.setText(mSelectedHousing.itemName);
						}
						mMasker.setVisibility(View.GONE);
					}
				});
				mOptions.show();
			}
			break;
			case R.id.apply_quota_next:
				StatService.onEvent(getContext(), StatIDSet.EVENT_ID_FILL_IN_BASIC_INFORMATION);
				if (!checkBaseInfo()) {
					return;
				}
				break;
			case R.id.no_network_layout:
				showLoading(true);
				mCustomerInfoPresenter.getCustomerInfo(Constants.CUSTOMER_TYPE_BASEINFO);
				break;
		}
	}

	private void showNotOpenRl() {
		mNotOpenRl.setVisibility(View.VISIBLE);
		showOrHideWork(View.GONE);
		showOrHideViews(View.GONE);
		showOrHideNext(View.GONE);
	}

	/**
	 * 允许继续申请额度
	 */
	private void enableNext() {
		mNotOpenRl.setVisibility(View.GONE);
		showOrHideWork(View.VISIBLE);
		if(R.id.apply_quota_work_radio_entrepreneur == mWork.getCheckedRadioButtonId() || R.id.apply_quota_work_radio_salaryman == mWork.getCheckedRadioButtonId()){
			showOrHideViews(View.VISIBLE);
		}
		showOrHideNext(View.VISIBLE);
	}

	/**
	 * 验证基本信息并提交
	 */
	private boolean checkBaseInfo() {
		if (!mAreaOpened) {
			showToast(R.string.quota_notOpen_hint1);
			CLog.i(TAG, getString(R.string.quota_notOpen_hint1));
			return false;
		}
		String work = "";
		if (R.id.apply_quota_work_radio_entrepreneur == mWork.getCheckedRadioButtonId()) {//企业主
			work = ENTREPRENEUR;
		} else if (R.id.apply_quota_work_radio_salaryman == mWork.getCheckedRadioButtonId()) {//上班族
			work = EMPLOYEE;
		}
		if (TextUtils.isEmpty(work)) {
			showToast(String.format(getString(R.string.please_select_toast), getString(R.string.job_type)));
			CLog.i(TAG, String.format(getString(R.string.please_select_toast), getString(R.string.job_type)));
			return false;
		}
		String hasHouse = "";
		if (R.id.apply_quota_house_have == mHouse.getCheckedRadioButtonId()) {//有本地房产
			hasHouse = HAVE;
		} else if (R.id.apply_quota_house_none == mHouse.getCheckedRadioButtonId()) {//无本地房产
			hasHouse = WITHOUT;
		}
		if (TextUtils.isEmpty(hasHouse)) {
			showToast(String.format(getString(R.string.please_select_toast), getString(R.string.local_house)));
			CLog.i(TAG, String.format(getString(R.string.please_select_toast), getString(R.string.local_house)));
			return false;
		}
		if (null == mSelectedEducation) {
			showToast(String.format(getString(R.string.please_select_toast), getString(R.string.education_degree)));
			CLog.i(TAG, String.format(getString(R.string.please_select_toast), getString(R.string.education_degree)));
			return false;
		}
		if (null == mSelectedMarriage) {
			showToast(String.format(getString(R.string.please_select_toast), getString(R.string.marital_status)));
			CLog.i(TAG, String.format(getString(R.string.please_select_toast), getString(R.string.marital_status)));
			return false;
		}
		if (null == mSelectedHousing) {
			showToast(String.format(getString(R.string.please_select_toast), getString(R.string.living_status)));
			CLog.i(TAG, String.format(getString(R.string.please_select_toast), getString(R.string.living_status)));
			return false;
		}
		if (!mCompanyDetail.validate()) {
			return false;
		}
		String company = mCompanyDetail.getText();
		PersonalBean baseInfo = new PersonalBean();
		baseInfo.addP = mOpenCity.provinceCode;//mSelectedProvince;
		baseInfo.addC = mOpenCity.cityCode;//mSelectedCity;
		baseInfo.addD = mOpenCity.districtCode;//mSelectedCounty;
		baseInfo.workType = work;
		baseInfo.hasHouse = hasHouse;
		baseInfo.education = mSelectedEducation.itemNo;
		baseInfo.marriage = mSelectedMarriage.itemNo;
		baseInfo.living = mSelectedHousing.itemNo;
		baseInfo.company = company;

		baseInfo.dept = "";
		baseInfo.title = "";
		mBaseInfoPresenter.saveBaseInfo(baseInfo);
		return true;
	}

	@Override
	public void saveBaseInfoSuccess() {
		((ApplyQuotaActivity) getActivity()).showFragment(ApplyQuotaActivity.PAGE_ID_CARD_SCAN, null);
	}

	@Override
	public void saveBaseInfoFail(String msg) {
		showToast(msg);
		CLog.i(TAG, "基本信息保存失败");
	}

	@Override
	public void customerInfoExist(String type, CustomerBean customerBean) {
		if(Constants.CUSTOMER_TYPE_BASEINFO.equals(type)){
			showReloadView(View.GONE);
			mServerData = customerBean.baseInfo;
			CLog.i(TAG, "获取个人信息成功-->" + mServerData.toString() + ";mInitCompleted=" + mInitCompleted);
			if (!mInitCompleted) {
				mBaseInfoPresenter.getAreaList();
			}
		}
	}

	@Override
	public void customerInfoNotExist(String type, String msg) {
		if(Constants.CUSTOMER_TYPE_BASEINFO.equals(type)){
			showReloadView(View.GONE);
			mServerData = null;
			CLog.i(TAG, "获取个人信息失败--获取定位信息mInitCompleted=" + mInitCompleted);
			if (!mInitCompleted) {
				mBaseInfoPresenter.getAreaList();
			}
		}
	}

	@Override
	public void getCustomerInfoError(String type, String msg) {
		if(Constants.CUSTOMER_TYPE_BASEINFO.equals(type)){
			showReloadView(View.VISIBLE);
		}
	}

	private void showBaseInfo() {
		CLog.i(TAG, "原有信息-showBaseInfo");
		PersonalBean bean;
		boolean isServerData = false;
		if (null != mServerData) {
			bean = mServerData;
			isServerData = true;
		} else {
			bean = mBaseInfo;
		}
		if (null != bean) {
			mSelectedProvince = bean.addP;
			mSelectedCity = bean.addC;
			mSelectedCounty = bean.addD;
			String type = "areaName";
			if (isServerData) {
				type = "areaId";
			}
			mOpenCity = mBaseInfoPresenter.judgeCity(mProvince, mCity, mCounty, mSelectedProvince, mSelectedCity, mSelectedCounty, type);//判断准入城市
			mAreaOpened = (null != mOpenCity);
			if (null != mOpenCity) {
				mAddressDetail.setText(mOpenCity.getAddress());
			}
			CLog.i(TAG, "原有信息-判断-isAreaOpen->" + mAreaOpened);
			if (mAreaOpened) {
				enableNext();
			} else {
				showNotOpenRl();
			}
			if (ENTREPRENEUR.equals(bean.workType)) {
				mRadioEntrepreneur.toggle();
			} else if (EMPLOYEE.equals(bean.workType)) {
				mRadioSalaryman.toggle();
				if (!TextUtils.isEmpty(bean.dept)) {
					mDepartmentDetail.setText(bean.dept);
				}
				if (!TextUtils.isEmpty(bean.title)) {
					mCompanyTitleDetail.setText(bean.title);
				}
			}
			if (WITHOUT.equals(bean.hasHouse)) {
				mHouse.check(R.id.apply_quota_house_none);
			} else if (HAVE.equals(bean.hasHouse)) {
				mHouse.check(R.id.apply_quota_house_have);
			}
			mSelectedEducation = OptionsUtil.getOptionBeanByValue(mEducations, bean.education);
			if (null != mSelectedEducation) {
				mEducation.setText(mSelectedEducation.itemName);
			}
			mSelectedMarriage = OptionsUtil.getOptionBeanByValue(mMarriages, bean.marriage);
			if (null != mSelectedMarriage) {
				mMarriage.setText(mSelectedMarriage.itemName);
			}
			mSelectedHousing = OptionsUtil.getOptionBeanByValue(mLivingStates, bean.living);
			if (null != mSelectedHousing) {
				mHousing.setText(mSelectedHousing.itemName);
			}
			if (!TextUtils.isEmpty(bean.company)) {
				mCompanyDetail.setText(bean.company);
			}
		} else {
			judgeAccessCity(false);
		}
	}

	private void judgeAccessCity(boolean needStat) {
		mSelectedProvince = mLocation.address.province;
		mSelectedCity = mLocation.address.city;
		mSelectedCounty = mLocation.address.district;
		mAddressDetail.setText(mSelectedProvince + " " + mSelectedCity + " " + mSelectedCounty);

		// 判断定位位置是否为开放区域
		mOpenCity = mBaseInfoPresenter.judgeCity(mProvince, mCity, mCounty, mLocation);//判断准入城市
		CLog.i(TAG, "judgeAccessCity-mOpenCity->" + mOpenCity.toString());
		mAreaOpened = (null != mOpenCity);
		if (mAreaOpened) {
			if(needStat){
				HashMap<String,String> map = new HashMap<>();
				map.put(StatService.ATTRIBUTE_KEY_ONE,mOpenCity.provinceCode);
				map.put(StatService.ATTRIBUTE_KEY_TWO,mOpenCity.cityCode);
				map.put(StatService.ATTRIBUTE_KEY_THREE,mOpenCity.districtCode);
				StatService.onEvent(getContext(), StatIDSet.EVENT_ID_ADDRESS_A_PERMANENT,map);
			}
			CLog.i(TAG, "开放城市-->");
			enableNext();
		} else {
			CLog.i(TAG, "非开放城市-->");
			showNotOpenRl();
		}
	}

	public void initArea(List<BaseInfoModel.AccessCity> areas) {
		mProvince.clear();
		mCity.clear();
		mCounty.clear();
		mBaseInfoPresenter.getAccessAddress(getActivity(), areas, mProvince, mCity, mCounty);
	}

	public void initOptions() {
		InitDataModel.Options bean = BaseInfoPresenter.getOptionList();
		if (null != bean) {
			mEducations.clear();
			mMarriages.clear();
			mLivingStates.clear();
			mEducations.addAll(bean.education);
			mMarriages.addAll(bean.marriage);
			mLivingStates.addAll(bean.living);
		} else {
			mEducations.clear();
			mMarriages.clear();
			mLivingStates.clear();
		}
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		unbinder.unbind();
	}

	@Override
	public void initAreaSuccess(List<BaseInfoModel.AccessCity> areas) {
		showReloadView(View.GONE);
		mInitCompleted = true;
		initArea(areas);
		initData();
	}

	@Override
	public void initAreaFail(String msg) {
		showReloadView(View.GONE);
		dismissLoading();
		mInitCompleted = false;
	}
	private void showReloadView(int visible) {
		if ((visible == View.GONE && mNoNetworkLayout.getVisibility() == View.VISIBLE)
				|| (visible == View.VISIBLE && mNoNetworkLayout.getVisibility() == View.GONE)) {
			mNoNetworkLayout.setVisibility(visible);
		}
	}

	@Override
	public void onFunctionalError(Throwable e, boolean autoTips, int functionalCode) {
		super.onFunctionalError(e, autoTips, functionalCode);
		switch (functionalCode) {
			case CustomerInfoModel.METHOD_CUSTOMER_INFO:
			case BaseInfoModel.METHOD_GET_SUPPORT_AREA:
				getActivity().runOnUiThread(new Runnable() {
					@Override
					public void run() {
						showReloadView(View.VISIBLE);
					}
				});
				break;
		}
	}
}